SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE VIEW dbo.V_PRODUCTOS_MOVIMIENTOS
AS
SELECT     'COMPRA' AS TIPO_MOVIMIENTO, COMPRAS.ID, CONDICIONES.CONDICION, COMPRAS.FECHA, RIGHT('000000' + CAST(COMPRAS.ID_SUPLIDOR AS VARCHAR(6)), 6) 
                      + ' - ' + SUPLIDORES.SUPLIDOR AS NOMBRE, ORIGINALES.NRO_ORIGINAL, PRODUCTOS.CODIGO, DESCRIPCIONES.DESCRIPCION, 
                      COMPRAS_DETALLES.CANTIDAD, COMPRAS_DETALLES.C_DEVUELTA, COMPRAS_DETALLES.COSTO, COMPRAS_DETALLES.PRECIO, COMPRAS.ACTIVO, 
                      COMPRAS.NRO_FACTURA AS NRO_DOCUMENTO
FROM         COMPRAS INNER JOIN
                      COMPRAS_DETALLES ON COMPRAS.ID = COMPRAS_DETALLES.ID INNER JOIN
                      PRODUCTOS ON COMPRAS_DETALLES.ID_PRODUCTO = PRODUCTOS.ID INNER JOIN
                      ORIGINALES ON PRODUCTOS.ID_ORIGINAL = ORIGINALES.ID INNER JOIN
                      SUPLIDORES ON COMPRAS.ID_SUPLIDOR = SUPLIDORES.ID INNER JOIN
                      DESCRIPCIONES ON ORIGINALES.ID_DESCRIPCION = DESCRIPCIONES.ID INNER JOIN
                      CONDICIONES ON COMPRAS.ID_CONDICION = CONDICIONES.ID
UNION ALL
SELECT     'FACTURA' AS TIPO_MOVIMIENTO, FACTURAS.ID, CONDICIONES.CONDICION, CONVERT(DATE, FACTURAS.FECHA_HORA, 103) AS FECHA, 
                      RIGHT('0000000' + CAST(FACTURAS.ID_CLIENTE AS VARCHAR(6)), 6) + ' - ' + CLIENTES.CLIENTE AS NOMBRE, ORIGINALES.NRO_ORIGINAL, PRODUCTOS.CODIGO, 
                      DESCRIPCIONES.DESCRIPCION, FACTURAS_DETALLES.CANTIDAD, FACTURAS_DETALLES.C_DEVUELTA, FACTURAS_DETALLES.COSTO, 
                      FACTURAS_DETALLES.PRECIO, FACTURAS.ACTIVO, CAST(FACTURAS.NRO_FACTURA AS VARCHAR) AS NRO_DOCUMENTO
FROM         FACTURAS INNER JOIN
                      FACTURAS_DETALLES ON FACTURAS.ID = FACTURAS_DETALLES.ID INNER JOIN
                      PRODUCTOS ON FACTURAS_DETALLES.ID_PRODUCTO = PRODUCTOS.ID INNER JOIN
                      ORIGINALES ON PRODUCTOS.ID_ORIGINAL = ORIGINALES.ID INNER JOIN
                      CLIENTES ON FACTURAS.ID_CLIENTE = CLIENTES.ID INNER JOIN
                      DESCRIPCIONES ON ORIGINALES.ID_DESCRIPCION = DESCRIPCIONES.ID INNER JOIN
                      CONDICIONES ON FACTURAS.ID_CONDICION = CONDICIONES.ID
UNION ALL
SELECT     'AJUSTE' AS TIPO_MOVIMIENTO, AJUSTES_INV.ID, AJUSTES_INV_TIPOS.TIPO AS CONDICION, CONVERT(DATE, AJUSTES_INV.FECHA, 103) AS FECHA, 
                      'N/A' AS NOMBRE, ORIGINALES.NRO_ORIGINAL, PRODUCTOS.CODIGO, DESCRIPCIONES.DESCRIPCION, 
                      AJUSTES_INV_DETALLES.CANTIDAD_AJUSTADA AS CANTIDAD, 0 AS C_DEVUELTA, AJUSTES_INV_DETALLES.COSTO, AJUSTES_INV_DETALLES.PRECIO, 
                      CAST(1 AS BIT) AS ACTIVO, CAST(AJUSTES_INV.ID AS VARCHAR) AS NRO_DOCUMENTO
FROM         AJUSTES_INV INNER JOIN
                      AJUSTES_INV_DETALLES ON AJUSTES_INV.ID = AJUSTES_INV_DETALLES.ID INNER JOIN
                      PRODUCTOS ON AJUSTES_INV_DETALLES.ID_PRODUCTO = PRODUCTOS.ID INNER JOIN
                      ORIGINALES ON PRODUCTOS.ID_ORIGINAL = ORIGINALES.ID INNER JOIN
                      DESCRIPCIONES ON ORIGINALES.ID_DESCRIPCION = DESCRIPCIONES.ID INNER JOIN
                      AJUSTES_INV_TIPOS ON AJUSTES_INV.ID_TIPO = AJUSTES_INV_TIPOS.ID
GO
EXEC sp_addextendedproperty N'MS_DiagramPane1', N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[5] 4[4] 2[84] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 9
         Width = 284
         Width = 1500
         Width = 1500
         Width = 4155
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
', 'SCHEMA', N'dbo', 'VIEW', N'V_PRODUCTOS_MOVIMIENTOS', NULL, NULL
GO
EXEC sp_addextendedproperty N'MS_DiagramPaneCount', 1, 'SCHEMA', N'dbo', 'VIEW', N'V_PRODUCTOS_MOVIMIENTOS', NULL, NULL
GO
